package com.yanqu.road.server.cmd.scene;

import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.entity.scene.UserScene;
import com.yanqu.road.pb.scene.SceneProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.scene.SceneMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_CROSS_SCENE_ACTION_SYNC, desc = "跨服同步场景内操作")
public class SyncSceneActionFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        SceneProto.SyncSceneActionRespMsg respMsg = SceneProto.SyncSceneActionRespMsg.parseFrom(packet.getBytes());
        int activityId = respMsg.getActivityId();
        List<SceneProto.SceneActionMsg> actionList = respMsg.getActionListList();

        // 分组
        Map<Integer, Map<String, List<SceneProto.SceneActionMsg>>> mapMap = new HashMap<>();
        for (SceneProto.SceneActionMsg actionMsg : actionList) {
            Map<String, List<SceneProto.SceneActionMsg>> map = mapMap.get(actionMsg.getSceneType());
            if (map == null) {
                mapMap.put(actionMsg.getSceneType(), new HashMap<>());
                map = mapMap.get(actionMsg.getSceneType());
            }
            List<SceneProto.SceneActionMsg> list = map.get(actionMsg.getSceneId());
            if (list == null) {
                map.put(actionMsg.getSceneId(), new ArrayList<>());
                list = map.get(actionMsg.getSceneId());
            }
            list.add(actionMsg);
        }

        // 同步
        for (Map<String, List<SceneProto.SceneActionMsg>> map : mapMap.values()) {
            for (List<SceneProto.SceneActionMsg> list : map.values()) {
                SceneProto.SyncSceneActionRespMsg.Builder builder = SceneProto.SyncSceneActionRespMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.addAllActionList(list);
                // 推送给在线玩家
                List<UserScene> sceneUserList = SceneMgr.getSceneUserList(activityId, list.get(0).getSceneType(), list.get(0).getSceneId());
                for (UserScene userScene : sceneUserList) {
                    GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userScene.getUserId());
                    if (onlinePlayer != null && onlinePlayer.getState() == PlayerState.ONLINE) {
                        onlinePlayer.sendPacket(ClientProtocol.U_SCENE_ACTION_SYNC, builder);
                    } else {
                        SceneMgr.quitScene(userScene.getUserId(), System.currentTimeMillis(), true);
                    }
                }
            }
        }
    }

}
